import { HttpClient } from '@angular/common/http';
import { HttpService } from 'src/app/shared/service/http.service';
import { AppGlobalService } from 'src/app/shared/service/app-global.service';
import { Component, OnInit, Input } from '@angular/core';
import { ModalController } from '@ionic/angular';
import { EventService } from 'src/app/shared/service/event.service';

@Component({
  selector: 'app-ms-code-modal',
  templateUrl: './ms-code-modal.component.html',
  styleUrls: ['./ms-code-modal.component.scss'],
})
export class MsCodeModalComponent implements OnInit {
  @Input() cssClass: any; // //  配置 <param name="modalCss" value="bottom"></param> 不配置全屏，bottom 底部弹出，其他值 居中
  @Input() cssStyle: any; // 改变默认小窗样式用
  @Input() item: any ;
  phone: any;
  time = -1;
  code: any;
  constructor(
    public modalController: ModalController,
    public eventService: EventService,
    public appGlobalService: AppGlobalService,
    private httpService: HttpService
  ) { }

  ngOnInit() {
    this.phone = this.item.submit.content[this.item.submit.content.phoneKey] + '';
    const reg = /^(.{3}).*(.{4})$/;
    this.phone = this.phone.replace(reg, "$1****$2");
  }
  onCancelClick(e) {
    e.stopPropagation();
  }
  dismissClick(i?) {
    this.item.submit.content[this.item.submit.content.msCodeKey] = '';
    if (i === 0) {
      this.modalController.dismiss({data: 0});
    } else {
      this.modalController.dismiss();
    }

  }

  changeValue(event) {

    if (event.detail.value.length > 6) {
      this.code = event.detail.value.slice(0, 6);
    } else {
      this.code = event.detail.value;
    }
    if (this.code.length  === 6) {

      this.item.submit.content[this.item.submit.content.msCodeKey] = this.code;
      // 表单内部提交
      if (this.item.formType === 'innerBtn') {
        this.eventService.eventEmit.emit("MsCodeSubmitInner",   JSON.stringify (this.item));
      } else {
        // 通用btn提交
        this.eventService.eventEmit.emit("MsCodeSubmit",   JSON.stringify (this.item));
      }

    }
  }

  getVC() {

    const phone = this.item.submit.content[this.item.submit.content.phoneKey];
    console.log('phone number:' + phone);
    // if (this.appGlobalService.isPhone(phone)) {
    //   this.appGlobalService.showToast('请输入正确手机号码', 2000, 'top');
    // } else 
    {
      let params: any;
      // if (this.showImgCode) {
      //   if (this.newForm.get('imgcode').errors) {
      //     this.appGlobalService.showToast('请输入图形验证码', 2000, 'top');
      //     return;
      //   } else {
      //     params = {
      //       phone: this.appGlobalService.encryptDes(this.newForm.value.phone),
      //       vcode: this.appGlobalService.encryptDes(this.newForm.value.imgcode),
      //       type: 0
      //     };
      //   }

      // } else
      {
        params = {
          phone: this.appGlobalService.encryptDes(phone + ""),
          type: 0,
          typeValue: 1
        };
      }

      this.httpService.post('userRegister/smsSend', params, data => {
        if (data.code === 0) {
          this.appGlobalService.showToast(
            '验证码已发送、请注意查收',
            2000,
            'top'
          );
          if (data.data) {
            this.code = data.data.code;
            this.item.submit.content[this.item.submit.content.msCodeKey] = data.data.code;
          }
          this.time = 90;
          const timer = setInterval(() => {
            this.time = this.time - 1;
            if (this.time === -1) {
              clearInterval(timer);
            }
          }, 1000);

          // if (this.newForm.contains('imgcode')) {
          //   this.newForm.removeControl('imgcode');
          //   this.showImgCode = false;
          // }
        } else if (data.code === 602) {
          // this.showImgCode = true;
          // if (!this.newForm.contains('imgcode')) {
          //   this.newForm.addControl('imgcode', new FormControl(null, [Validators.minLength(4), Validators.maxLength(4)]));
          // }
          // this.getImgCode();
          this.appGlobalService.showToast(data.message, 3000, 'top');
        } else {
          this.appGlobalService.showToast(data.message, 3000, 'top');
        }
      });
    }
  }
}
